home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / editor / editor2 / htmlttxd.lha / HTMLtoTeXAid.rexx < prev    next >
OS/2 REXX Batch file  |  1996-12-30  |  6KB  |  204 lines

  1. /* Convert `*xyz*' i.e. bold emphasis to `jmsempth{xyz}'              */
  2. /* and {\tt{}"} pairs to `` and ''                                    */
  3. /* both of these functions are to aid conversion of html to TeX files */
  4. /* Written by David Fong 9th December 1996                            */
  5. /* Version 1.0 30th December 1996                                     */
  6. parse arg filenames
  7. parse var filenames infile ' ' outfile
  8.  
  9. say 'HTML to TeX aid'
  10. say "Convert `*xyz*' i.e. bold emphasis to `jmsempth{xyz}'"
  11. say "and {\tt{}\" || '22'x || "} pairs to `` and ''"
  12. say "both of these functions are to aid conversion of html to TeX files"
  13. say "Written by David Fong 9th December 1996"
  14. say ''
  15.  
  16. n_strings=2     /* this can be modified, but the number of data sets
  17.                    below then needs to be modified as well */
  18.  
  19. /* data sets */
  20.  
  21. find_string.1='*'
  22. repl_strng1.1='\jmsempth{'
  23. repl_strng2.1='}'
  24.  
  25. flag.1=false
  26. start_line.1=0
  27. end_line.1=0
  28. start_pos.1=0
  29. end_pos.1=0
  30.  
  31. find_string.2='{\tt{}\"}'
  32. repl_strng1.2="``"
  33. repl_strng2.2="''"
  34.  
  35. flag.2=false
  36.  
  37. start_line.2=0  /* line of first quote marker */
  38. end_line.2=0    /* line of second quote marker */
  39. start_pos.2=0
  40. end_pos=0
  41.  
  42. /* start of program proper */
  43.  
  44. line_count=0        /* number of lines read */
  45. written_line=0      /* number of lines written */
  46.  
  47. if arg() ~= 1 | outfile == '' | infile == '' then do
  48.  say 'Incorrect number of arguments'
  49.  say 'Usage : HTMLtoTeXAid infile outfile'
  50.  exit
  51. end
  52.  
  53. if open('in',infile,'r') then do
  54.  if open('out',outfile,'w') then do
  55.   do while ~eof('in')
  56.    line_count=line_count+1  /* current line being read */
  57.    a.line_count=readln('in')
  58.    do i=1 to n_strings
  59.     pos.i=0
  60.     still_search.i=true
  61.    end                      /* prime these markers to start of string */
  62.    do until sum_flag=false
  63.     do i=1 to n_strings
  64.      if still_search.i=true then do
  65.       pos.i=index(a.line_count,find_string.i,pos.i+1)
  66.       if pos.i>0 then
  67.        call handle_marker(i)
  68.       else
  69.        still_search.i=false
  70.      end
  71.     end
  72.     
  73.     sum_flag=false
  74.     do i=1 to n_strings     /* loop until we find no more markers */
  75.      if pos.i>0 then
  76.       sum_flag=true
  77.     end
  78.    end
  79.    call output_lines()      /* write as many lines as we can */
  80.   end
  81.   if line_count>written_line then do /* output the rest of the lines, we have stopped reading */
  82.    do i=written_line+1 to line_count
  83.     writeln('out',a.i)
  84.     a.i=''
  85.    end
  86.   end
  87.   close('out')
  88.  end
  89.  else
  90.   say 'Could not open "'outfile'" for writing.'
  91.  close('in')
  92. end
  93. else
  94.  say 'Could not open "'infile'" for reading.'
  95.  
  96. say ''
  97. exit
  98.  
  99. output_lines: procedure expose a. line_count n_strings flag. start_line. written_line
  100.  
  101.  write_to_line=line_count
  102.  
  103.  do i=1 to n_strings      /* write up to the line which we are still working on! */
  104.   if flag.i=true & (write_to_line>=start_line.i) then
  105.   write_to_line=start_line.i-1
  106.  end
  107.  
  108.  if write_to_line>written_line then do
  109.   do i=written_line+1 to write_to_line
  110.    writeln('out',a.i)
  111.    a.i=''
  112.   end
  113.   written_line=write_to_line
  114.  end
  115.  
  116. return
  117.  
  118. handle_marker: procedure expose flag. start_pos. pos. end_pos. start_line. line_count a. n_strings find_string. repl_strng1. repl_strng2.
  119.  
  120.  s=arg(1)           /* which strings are we replacing? */
  121.  
  122.  if flag.s=false then do
  123.   flag.s=true
  124.   start_pos.s=pos.s
  125.   start_line.s=line_count
  126.  end
  127.  else do
  128.   flag.s=false
  129.   end_pos.s=pos.s
  130.   do i=start_line.s to line_count
  131.    writech('STDOUT',' '||i||' ') /* print out the line numbers and text to be `converted' */
  132.    if start_line.s=line_count then /* both markers on the same line */
  133.     say left(delstr(a.i,1,start_pos.s-1),end_pos.s+length(find_string.s)-start_pos.s)
  134.    else if i=start_line.s then /* first line */
  135.     say delstr(a.i,1,start_pos.s-1)
  136.    else if i=line_count then      /* last line */
  137.     say left(a.i,end_pos.s+length(find_string.s)-1)
  138.    else
  139.     say a.i
  140.   end
  141.   writech('STDOUT','Convert?:')
  142.   pull response
  143.   if length(response)>0 then
  144.    response=left(upper(response),1)
  145.   if (response=='') | (response=="Y") then do /* `no' response is taken as yes */
  146.    zz=start_line.s
  147.    a.zz=delstr(a.zz,start_pos.s,length(find_string.s))
  148.    a.zz=insert(repl_strng1.s,a.zz,start_pos.s-1)
  149.    call adjust_markers1(s) /* marker positions may have just been changed */
  150.                            /* end_pos.s may have been changed by previous line */
  151.    a.line_count=delstr(a.line_count,end_pos.s,length(find_string.s))
  152.    a.line_count=insert(repl_strng2.s,a.line_count,end_pos.s-1)
  153.    call adjust_markers2(s) /* marker positions may have just been changed again! */
  154.    do i=start_line.s to line_count
  155.     say '-->'||a.i
  156.    end
  157.   end
  158.   else do
  159.    /* a difficult situation to handle */
  160.    /* what does the user actually mean, to ignore both markers or just the first one? */
  161.    /* to increase flexibility, we take it to mean to ignore the first one */
  162.    flag.s=true
  163.    start_pos.s=end_pos.s
  164.    start_line.s=line_count
  165.   end
  166.  end
  167. return
  168.  
  169. adjust_markers1: procedure expose pos. start_pos. line_count n_strings start_line. end_pos. repl_strng1. find_string.
  170.  
  171.  s=arg(1)
  172.  
  173.  do i=1 to n_strings
  174.   if start_line.s=start_line.i then do
  175.    if start_pos.i>start_pos.s then
  176.     start_pos.i=start_pos.i+length(repl_strng1.s)-length(find_string.s)
  177.    /* all markers so far found have either been stored as start_pos etc.,
  178.       or have already been used to `close' their expression,
  179.       with the exception of the marker just found (see below) */
  180.    if pos.i>start_pos.s then
  181.     pos.i=pos.i+length(repl_strng1.s)-length(find_string.s)
  182.   end
  183.  end
  184.  if start_line.s=line_count then /* handle the marker just found */
  185.   end_pos.s=end_pos.s+length(repl_strng1.s)-length(find_string.s)
  186.  
  187. return
  188.  
  189. adjust_markers2: procedure expose pos. n_strings start_pos. line_count start_line. start_pos. repl_strng2. find_string.
  190.  
  191.  s=arg(1)
  192.  
  193.  do i=1 to n_strings
  194.   if line_count=start_line.i then do
  195.    if start_pos.i>end_pos.s then
  196.     start_pos.i=start_pos.i+length(repl_strng2.s)-length(find_string.s)
  197.    if pos.i>end_pos.s then
  198.     pos.i=pos.i+length(repl_strng2.s)-length(find_string.s)
  199.   end
  200.  end
  201.  
  202. return
  203.  
  204.